﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using AfterlifeLib.Input;

namespace AfterlifeLib.GameScreens.Menus
{
    public enum SliderDirection
    {
        Left,
        Right,
    }

    public class ValueChangedEventArgs : EventArgs
    {
        public float Value { get; set; }

        public ValueChangedEventArgs(float value)
        {
            Value = value;
        }
    }

    public class SliderMenuEntry : MenuEntry
    {
        public static Texture2D SliderBackgroundFiller
        {
            get;
            set;
        }

        public static Texture2D SliderForegroundFiller
        {
            get;
            set;
        }

        static Vector2 sliderOffset = new Vector2(500, 0);
        public static Vector2 SliderOffset
        {
            get { return sliderOffset; }
            set { sliderOffset = value; }
        }

        static float sliderWidth = 200f;
        public static float SliderWidth
        {
            get { return sliderWidth; }
            set { sliderWidth = value; }
        }

        float minValue;
        float maxValue;

        float curValue;
        private float Value
        {
            get { return curValue; }
            set
            {
                curValue = MathHelper.Clamp(value, minValue, maxValue);
            }
        }

        public float GetDeltaValue
        {
            get { return (maxValue - minValue) / 10; }
        }

        public float SliderX
        {
            get
            {
                return (Value / (maxValue - minValue)) * sliderWidth;
            }
        }

        public event EventHandler<ValueChangedEventArgs> ValueChanged;

        public SliderMenuEntry(string text, float minValue, float maxValue, float value)
            : base(text)
        {
            this.minValue = minValue;
            this.maxValue = maxValue;
            this.curValue = value;
        }

        public override void HandleInput(MenuScreen screen)
        {
            if (InputState.IsKeyFirstDown(Keys.Left))
            {
                MoveSlider(SliderDirection.Left);
            }
            else if (InputState.IsKeyFirstDown(Keys.Right))
            {
                MoveSlider(SliderDirection.Right);
            }
        }

        public override void Draw(MenuScreen screen, Vector2 position, bool isSelected, GameTime gameTime)
        {
            Color color = isSelected ? SelectedColor : NormalColor;

            color = new Color(color.R, color.G, color.B, screen.TransitionAlpha);

            SpriteBatch spriteBatch = screen.ScreenManager.SpriteBatch;
            SpriteFont font = screen.ScreenManager.Font;

            Vector2 origin = new Vector2(0, font.LineSpacing / 2);
            spriteBatch.DrawString(font, Text, position, color, 0,
                                   origin, 1f, SpriteEffects.None, 0);

            Vector2 sliderBackgroundOrigin = new Vector2(0, SliderBackgroundFiller.Height / 2);
            Vector2 sliderOrigin = new Vector2(0, SliderForegroundFiller.Height / 2);

            spriteBatch.Draw(
                SliderBackgroundFiller, 
                new Rectangle(
                    (int)(position.X + ValueOffset.X), 
                    (int)position.Y, 
                    (int)SliderWidth, 
                    SliderBackgroundFiller.Height),
                null,
                Color.White,
                0,
                sliderBackgroundOrigin,
                SpriteEffects.None,
                0);

            spriteBatch.Draw(
                SliderForegroundFiller,
                new Rectangle(
                    (int)(position.X + SliderOffset.X),
                    (int)position.Y,
                    (int)SliderX,
                    SliderForegroundFiller.Height),
                null, 
                Color.White, 
                0, 
                sliderOrigin, 
                SpriteEffects.None, 
                0);
        }

        private void MoveSlider(SliderDirection direction)
        {
            switch (direction)
            {
                case SliderDirection.Left:
                        Value -= GetDeltaValue;
                    break;
                case SliderDirection.Right:
                        Value += GetDeltaValue;
                    break;
            }

            OnValueChanged();
        }

        private void OnValueChanged()
        {
            if (ValueChanged != null)
                ValueChanged(this, new ValueChangedEventArgs(Value));
        }
    }
}
